<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: xajax.product_images.php 283 2013-09-23 09:43:42Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 283 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-23 09:43:42 +0000 (Mon, 23 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

function getProductImages($values) {
	global $db;

	$ajax = new xajaxResponse();
	$get = urldecode($values);

  if (preg_match('/[^0-9\|]+/', $get)) { return $ajax; exit(); }
  $ids = explode('|', $get);

  $sql = "
    select
    	img.products_images_id, img.products_id,
    	group_concat(img.products_options_values_id order by opt.products_options_id separator '|') att_ids,
    	group_concat(opt.products_options_id order by opt.products_options_id separator '|') opt_ids,
      ifnull(prod_img.image_name, prod.products_image) image_name
    from
    	mercari_products_options_values_to_products_images img
    	inner join mercari_products_options_values_to_products_options opt_rl on img.products_options_values_id = opt_rl.products_options_values_id
    	inner join mercari_products_options opt on opt_rl.products_options_id = opt.products_options_id
    	left join mercari_products_images prod_img on img.products_images_id = prod_img.image_id
    	left join mercari_products prod on img.products_images_id = 0 and img.products_id = prod.products_id
    where
    	opt.products_options_id in ('".implode("', '", $ids)."')
    group by
    	img.products_images_id, img.products_id";

//  echo $sql;
  $image_attributes = array();
  $images = $db->db_query($sql);
	while(!$images->EOF) {
    $image_attributes[] = "'" . $images->fields['att_ids'] . "': '" . $images->fields['image_name'] . "'";
    $images->MoveNext();
  }
  
	$ajax->script('
    SMR_Image_Options.images = {'.implode(', ', $image_attributes) . '};
    SMR_Image_Options.changeImage();
  ');

	return $ajax;
}

if(!is_object($xajax))
	$xajax = new xajax();

$xajax->registerFunction("getProductImages");